﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace Quality.BasicData
{
    public partial class equipmentsedit : Form
    {
        public String id { get; set; }
        public equipmentsedit(string id)
        {
            this.id = id;
            InitializeComponent();
        }
 private void equipmentsedit_Load(object sender, EventArgs e)
        {
           if (id == "")
            {
                num.Text = "";
                eqname.Text = "";


            }
            else
            {
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = new SqlCommand();

                da.SelectCommand.CommandText = "select a.id,a.num,a.eqname,a.worknum,a.orgnum,b.nodeName,c.workname,a.stop from  equipments a left join organization b on a.orgnum=b.id left join workcenters c on a.worknum=c.num where a.id=@id";
                da.SelectCommand.Parameters.AddWithValue("@id", id);
                DataSet dt = DB.getlist(da);
                DataRow row = dt.Tables[0].Rows[0];
                num.Text = row["num"].ToString();
                eqname.Text = row["eqname"].ToString();
                orgnum.Text = row["nodeName"].ToString();
                orgnum.Tag = row["orgnum"].ToString();
                DB.getcombobox1(worknum, "workcenters", "num", "workname", "orgnum", orgnum.Tag.ToString());
                worknum.SelectedValue = row["worknum"].ToString();
                stopcheckBox.Checked = Convert.ToBoolean(row["stop"]);
            }
        }
     
        public event TransfDelegate1 TransfEvent;
        private void savebutton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(num.Text)) { MessageBox.Show("编号不能为空！"); return; }
            if (string.IsNullOrEmpty(eqname.Text)) { MessageBox.Show("设备名称不能为空！"); return; }
            if (string.IsNullOrEmpty(worknum.Text)) { MessageBox.Show("工作中心不能为空！"); return; }
            if (string.IsNullOrEmpty(orgnum.Text)) { MessageBox.Show("部门不能为空！"); return; }
            SqlCommand comm = new SqlCommand();
            if (id == "")
            {
                //int count = sqlHelper.Count("select * from testtypes  where num='" + num.Text + "'");
                //if (count > 0) { MessageBox.Show("编号重复，不能保存！"); return; }
                comm.CommandText = "insert into equipments(num,eqname,worknum,orgnum,stop)values(@num,@eqname,@worknum,@orgnum,@stop)";
            }
            else
            {

                comm.CommandText = "update equipments set num=@num,eqname=@eqname,worknum=@worknum,orgnum=@orgnum,stop=@stop where id=@id";


            }

            comm.Parameters.AddWithValue("@id", id);
            comm.Parameters.AddWithValue("@num", num.Text);
            comm.Parameters.AddWithValue("@eqname", eqname.Text);
            comm.Parameters.AddWithValue("@worknum", (worknum.SelectedIndex == -1) ? "NULL" : worknum.SelectedValue);
            comm.Parameters.AddWithValue("@orgnum", orgnum.Tag==null?"":orgnum.Tag);
            comm.Parameters.AddWithValue("@stop", stopcheckBox.Checked);
            DB.dosql(comm);
            TransfEvent();
            MessageBox.Show("保存成功！");
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            selectorg frm = new selectorg();

            frm.WindowState = FormWindowState.Normal;
            frm.Dock = DockStyle.Fill;
            frm.TransfEvent += frm_TransfEvent;
            frm.Show();
        }
        //事件处理方法
        void frm_TransfEvent(string[] value)
        {
            orgnum.Tag = value[0];
            orgnum.Text = value[1];

            DB.getcombobox1(worknum, "workcenters", "num", "workname", "orgnum", value[0]);







        }
    }
}
